<?php
class Page extends Action {
	/* (non-PHPdoc)
	 * @see Action::execute()
	 */
	public function execute($params) {
		$me = check_login ( ADMIN_LOGIN_PAGE );
		$data = array ();
		$page_id = irqst ( 'pid', 0 ); //新建
		//分类
		$cM = new WebPageCategory ();
		$data ['treeNodes'] = json_encode ( $cM->getTreeNodes ( 0 ) );
		//模板
		$data ['tplNodes'] = json_encode ( WebTemplateModel::getTemplateTreeData () );
		//标记
		$tagM = new WebTagModel ();
		$tpM = new WebPageTagModel ();
		$flags = $tagM->getFlags ();
		$data ['flags'] = $flags ? $flags : array ();
		$data ['content_modes'] = apply_filter ( 'get_content_mode', array (''=>'-请选择-') );
		if (empty ( $page_id )) {
			//页面的内容模型
			$cmode = rqst ( 'cmode', 'default' );
			$ctype = rqst ( 'ctype', 'page' );
			$page ['page_mode'] = $cmode;
			$page ['page_type'] = $ctype;
			$page ['category'] = rqst ( 'category' );
			$page ['tags'] = array ();
			$page ['flags'] = array ();
			$page = apply_filter ( 'get_default_page', $page );
		} else {
			$pM = new WebPageModel ();
			$page = $pM->read ( $page_id );
			if ($page) {
				$cmode = $page ['page_mode'];
				$tags = $tpM->getPageTags ( $page_id );
				$page ['tags'] = $tags ? $tags : array ();
				$flags = $tpM->getPageFlags ( $page_id );
				$page ['flags'] = $flags ? $flags->toArray ( 'tag_id', 'tag_id' ) : array ();
				$page ['category_name'] = $cM->getCategoryTitle ( $page ['category'] );
				$page ['subcategory_name'] = $cM->getCategoryTitle ( $page ['subcategory'] );
				//$page ['content'] = htmlspecialchars_decode ( $page ['content'] );
				$page ['content'] = $page ['content'];
			} else {
				Response::errorPage ( "页面不存在.", $this->referer );
			}
		}
		//页面信息
		$page = apply_filter ( 'on_edit_page', $page );
		$page = apply_filter ( 'on_edit_page_' . $cmode, $page );
		$data ['page'] = $page;
		//页面类型
		$types = apply_filter ( 'get_page_types', array ('' => '-请选择-' ) );
		$data ['type_options'] = $types;
		$page_custom_tpls = apply_filter ( "page_custom_tpls_for_{$cmode}", array () );
		$data ['page_head_tpl'] = isset ( $page_custom_tpls ['head'] ) ? $page_custom_tpls ['head'] : false;
		$data ['page_form_tpl'] = isset ( $page_custom_tpls ['form'] ) ? $page_custom_tpls ['form'] : false;
		$data ['page_foot_tpl'] = isset ( $page_custom_tpls ['foot'] ) ? $page_custom_tpls ['foot'] : false;
		$data ['navi_active'] = '<i class="icon-edit"></i>网页编辑器';
		$data ['hide_navi'] = rqset ( '_non' ) || ! empty ( $page ['page_id'] );
		return new SmartyView ( $data, 'vfs/Page' );
	}
}